package com.gewu.bookeeping.View.fragment;

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.bookeeping.MainActivity;
import com.gewu.bookeeping.MyDb.bean.UserBeanIn;
import com.gewu.bookeeping.MyDb.dao.UserDaoPerson;
import com.gewu.bookeeping.OnClick.fourItemClieck;
import com.gewu.bookeeping.R;
import com.gewu.bookeeping.Utils.AppCot;
import com.gewu.bookeeping.View.activity.LoginActivity;
import com.gewu.bookeeping.View.activity.aboutActivity;
import com.gewu.bookeeping.View.activity.fenxiActivity;
import com.gewu.bookeeping.View.activity.settingactivity;
import com.gewu.bookeeping.View.activity.userActivity;
import com.gewu.bookeeping.View.adapter.fourAdapter;
import com.makeramen.roundedimageview.RoundedImageView;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.util.List;

import static android.app.Activity.RESULT_OK;

public class fourFragment extends Fragment implements fourItemClieck {

    private Toolbar toolbar;
    private TextView title;
    private RecyclerView recliview;
    private RoundedImageView img;
    private TextView text;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.activity_four_fragment, container, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View inflater) {
        toolbar = (Toolbar) inflater.findViewById(R.id.toolbar);
        title = (TextView) inflater.findViewById(R.id.title);
        recliview = inflater.findViewById(R.id.recliview);
        toolbar.setTitle("个人中心");
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 1);
        recliview.setLayoutManager(gridLayoutManager);
        recliview.setAdapter(new fourAdapter((fourItemClieck) this));


        img = inflater.findViewById(R.id.img);
        text = (TextView) inflater.findViewById(R.id.text);

        text.setText(AppCot.name);

        inflater.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppCot.showDilog(getActivity(), "提示", "从相册选取照片").setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (ContextCompat.checkSelfPermission(getContext(),
                                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
                        } else {
                            selectPic();
                        }
                    }
                }).show();
            }
        });
    }

    @Override
    public void onItemClieck(int index) {
        switch (index) {
            case 0:
                //跳转到账户分析页面
                startActivity(new Intent(getContext(), fenxiActivity.class));
                break;
            case 1:
                startActivity(new Intent(getContext(), aboutActivity.class));
                break;
            case 2:
                //跳转到关于开发者
                startActivity(new Intent(getContext(), settingactivity.class));
                break;
            case 3:
                AppCot.isok(getContext(), false);
                startActivity(new Intent(getContext(), LoginActivity.class));
                break;
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 2:
                //获取选中文件的定位符
                Uri uri = data.getData();
                //使用content的接口
                ContentResolver cr = getActivity().getContentResolver();
                try {
                    //获取图片
                    Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                    img.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }

    }

    /**
     * 打开本地相册选择图片
     */
    private void selectPic() {
        //intent可以应用于广播和发起意图，其中属性有：ComponentName,action,data等
        Intent intent = new Intent();
        intent.setType("image/*");
        //action表示intent的类型，可以是查看、删除、发布或其他情况；我们选择ACTION_GET_CONTENT，系统可以根据Type类型来调用系统程序选择Type
        //类型的内容给你选择
        intent.setAction(Intent.ACTION_GET_CONTENT);
        //如果第二个参数大于或等于0，那么当用户操作完成后会返回到本程序的onActivityResult方法
        startActivityForResult(intent, 2);
    }

}